package com.justech.eat.service.component.impl;

import com.justech.eat.service.component.UserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * @program: eat-service
 * @description:
 * @author: feifei.li
 * @create: 2020-06-15 15:50
 **/
@Primary
@Component
public class UserTokenServiceImpl implements UserTokenService {

    @Qualifier("webUserTokenService")
    @Autowired
    UserTokenService webUserTokenService;

    @Qualifier("nativeUserTokenService")
    @Autowired
    UserTokenService nativeUserTokenService;

    /**
     * 获取用户信息
     *
     * @param token 已加密token
     * @return
     */
    @Override
    public String getJobNumber(String token) {
        String clientType = getClientType(token);
        String jobNumber = null;
        if (NATIVE.equals(clientType)) {
            try {
                jobNumber = nativeUserTokenService.getJobNumber(token);
            } catch (Exception e) {
                try {
                    jobNumber = webUserTokenService.getJobNumber(token);
                } catch (Exception e1) {
                    throw new RuntimeException("[" + WEB + "]用户身份解密失败" + e1.getMessage());
                }
            }
        } else {
            try {
                jobNumber = webUserTokenService.getJobNumber(token);
            } catch (Exception e) {
                try {
                    jobNumber = nativeUserTokenService.getJobNumber(token);
                } catch (Exception e1) {
                    throw new RuntimeException("[" + NATIVE + "]用户身份解密失败" + e1.getMessage());
                }
            }
        }
        return jobNumber;
    }
}
